package com.xz.xzmybatisagent;

import java.lang.instrument.Instrumentation;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/3/19-03-19 19:59
 */
public class AgentmainAgent {
    public static void agentmain(String agentArgs, Instrumentation inst) {
        System.out.println("开始执行 AgentmainAgent 代理程序...");
        InterceptorClassFileTransformer interceptorClassFileTransformer = new InterceptorClassFileTransformer();
        // 在代理参数中获取进程通信的 port
        interceptorClassFileTransformer.setArgs(agentArgs);
        inst.addTransformer(interceptorClassFileTransformer, true);

        // 主动触发retransform
        Class<?>[] classes = inst.getAllLoadedClasses();
        for (Class<?> clazz : classes) {
            if (clazz.getName().equals("org.apache.ibatis.plugin.InterceptorChain")) {
                System.out.println("agentmain ------------- retransformClasses");
                try {
                    inst.retransformClasses(clazz);
                } catch (Throwable t) {
                    System.err.println("agentmain ------------- retransformClasses error: " + t.getMessage());
                    // start-body
                }
            }
        }
    }
}
